﻿using Company.Application.Share.Events;
using Company.Application.Share.Model;
using Company.Application.Share.Prism;
using Company.Core.Ioc;
using Prism.Commands;
using Prism.Events;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Shell.ViewModel
{
    internal class MainWindowViewModel : BindableBase
    {
        public ICommand LoadedCommand { get; }
        public IModuleManager ModuleManager { get; set; }
        public IRegionManager RegionManager { get; set; }
        public MainWindowViewModel(IModuleManager moduleManager, IRegionManager regionManager)
        {
            ModuleManager = moduleManager;
            RegionManager = regionManager;
            LoadedCommand = new DelegateCommand(Loaded);
        }
        private void Loaded()
        {
            IRegion mainRegion = PrismProvider.RegionManager.Regions[RegionNames.MainRegion];
            mainRegion.NavigationService.Navigated += NavigationService_Navigated;
            //第一步，加载模块
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationLoginModule);
            //第二部，导航区域
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion,"LoginView");

            //订阅用户登录成功事件
            PrismProvider.EventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(OnLogined,ThreadOption.UIThread);
        }
        private void OnLogined(CurrentUser user)
        {
         
            //加载主界面
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationMainModule);
            //导航到主区域
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);
        }
        private Window MainWindow { get; set; } = Application.Current.MainWindow;
        private void NavigationService_Navigated(object sender, RegionNavigationEventArgs e, WindowStyle windowStyle)
        {
            switch (e.Uri.OriginalString) {
                case ViewNames.LoginView:
                    MainWindow.ResizeMode = ResizeMode.NoResize;
                    MainWindow.SizeToContent = SizeToContent.WidthAndHeight;
                    MainWindow.WindowState = WindowState.Normal;

                    MainWindow.Width = 600;
                    MainWindow.Height = 350;
                    MainWindow.WindowStyle = WindowStyle.None;
                    break;
                 case ViewNames.MainView:
                   MainWindow.Width = 1920;
                    MainWindow.Height = 1020;
                    MainWindow.ResizeMode = ResizeMode.CanResize;
                    MainWindow.SizeToContent = SizeToContent.Manual;
                    MainWindow.WindowState = WindowState.Maximized;
                    MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                    break;
                    default:
                    break;
            }
        }

        private string title = "上位机开发";
        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
    }
}
